﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolCode.Entities;
using CoolCode.Services;
using CoolCode.Web;
using CoolCode.Linq;

namespace CoolCode.Web.UI.Views.IC
{
    public partial class ProductOut : AuthPage
    {
        private IInspectionService _Service;
        public IInspectionService Service
        {
            get
            {
                if (_Service == null)
                {
                    _Service = ServiceFactory.Produce<IInspectionService>();
                }
                return _Service;
            }
        } 

        protected void Page_Load(object sender, EventArgs e)
        {
            handleEvents();
            if (!IsPostBack)
            {
                // BindData_Master();
                //gvMaster.Visible = false;
                productList.Visible = false;
                cdrOutbound.Value = DateTime.Now.Date;
                cdrCheckDateFr.Value = DateTime.Now.Date.AddMonths(-12);
                cdrCheckDateTo.Value = DateTime.Now.Date;
            }
        }

        protected void handleEvents()
        {
            // gvMaster.RowDataBound += new GridViewRowEventHandler(gvMaster_RowDataBound); 
            //gvMaster.HandleSelectAll("chkSelectAll", "chkSelected");
            lnkSearch.Click += new EventHandler(lnkSearch_Click);
            lnkSave.Click += new EventHandler(lnkSave_Click);
        }

        public void lnkSave_Click(object sender, EventArgs e)
        { 
            List<string> selected = productList.GetSelections();
            if (selected.Count == 0)
            {
                udpHeader.Alert("请选中需要出库的产品，然后点【出库】");
                return;
            }
            if (!cdrOutbound.Value.Vailidate())
            {
                udpHeader.Alert("出库日期：无效日期，请选择出库日期"  );
                return;
            }
            Guid guid = this.Service.Outbound(selected, UserName, DateTime.Now.Date, null);
            this.View<ProductOutResult>(new { key = guid }); 
        }

        protected void lnkSearch_Click(object sender, EventArgs e)
        {
            BindData_Master();
            //gvMaster.Visible = true;
            udpHeader.Update();
        }

        private void BindData_Master()
        {
            var qb = QueryBuilder.Create<Inspection>()
                 .Fuzzy(c => c.ProductID, txtProductID.Text)
                 .Fuzzy(c => c.ProductSpec, txtProductSpec.Text)
                 .Between(c => c.CheckDate, cdrCheckDateFr.Value.GetValueOrDefault(), cdrCheckDateTo.Value.GetValueOrDefault())
                 .Custom(c => c.Status == ProductStatus.In.GetAttachedString());
            var list = Service.List(qb);
            //gvMaster.DataSource = list;
            //gvMaster.DataBind();
            const int threshold = 200;
            if (list.Count > threshold)
            {
                udpHeader.Alert(string.Format("由于搜索出来的数据过多({0}条)，系统只显示前{1}条数据", list.Count, threshold));
                list = list.Take(threshold).ToList();
            }

            productList.Model = list;
            productList.DataBind();
            productList.Visible = true;

            List<string> valueList = new List<string>();
            string[] splits = txtProductID.Text.Split(',');
            if (splits.Length > 1)
            {
                splits.ForEach(c =>
                {
                    string el = c.Trim(); if (el.Length > 0)
                        valueList.Add(el);
                });
                var productIDList = list.Select(c => c.ProductID);
                var diff = valueList.Except(productIDList).ToList();
                if (diff.Count > 0)
                {
                    ltrSearchMessage.Text = "找不到以下产品编号：" + string.Join(", ", diff.ToArray());
                }
            }
        }

    }
}
